//
//  ClubHomeAlertView.swift
//  77Club
//
//  Created by Apple on 2024/9/24.
//

import UIKit
//import kf

class ClubHomeAlertView: UIView, RaeAnimationProtocol, RaeAnimationDelegate {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: .screenHeight)
    }
    
    func animationContainerViewStyle() -> RaeAnimationViewStyle {
        .bottom
    }
    
    var model: ClubHomeAlertModel? {
        didSet{
            guard let md = model else { return }
            gameImageView.kf.setImage(with: URL(string: md.image), completionHandler: { result in
                switch result {
                case .success(let value):
                    self.gameImageView.snp.remakeConstraints { make in
                        make.left.right.equalToSuperview()
                        make.centerY.equalToSuperview()
                        make.height.equalTo(self.gameImageView.snp.width).multipliedBy((value.image.size.height)/(value.image.size.width))
                    }
                case .failure(let error):
                    print("Error loading image: \(error.localizedDescription)")
                }
            })
        }
    }
    var toGameDetailBlock: ((_ gid: String) -> Void)?
    
    private let gameImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let closeButton = c_make_ex(UIButton.self)
        .image("Home_alertClose").end()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            gameImageView
            closeButton
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        closeButton.snp.makeConstraints { make in
            make.right.equalTo(-13.0.Scale)
            make.top.equalTo(gameImageView.snp.top).offset(-34)
            make.width.equalTo(26.0.Scale)
            make.height.equalTo(69.0.Scale)
        }
        
        let gameG = UITapGestureRecognizer(target: self, action: #selector(clickGameView))
        gameImageView.isUserInteractionEnabled = true
        gameImageView.addGestureRecognizer(gameG)
        closeButton.addTarget(self, action: #selector(clickCloseButton), for: .touchUpInside)
    }
    
    @objc private func clickCloseButton(){
        self.entryAnimate.animation(dismiss: self)
    }
    
    @objc private func clickGameView(){
        self.entryAnimate.animation(dismiss: self) {
            guard let gid = self.model?.gid, gid != "0" else { return }
            self.toGameDetailBlock?(gid)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
